﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VideoClassLibrary.Model;
using VideoClassLibrary.BusinessLogicLayer;
public partial class UserManagement_ViewUserInfoList : AdminUserBase
{
    public string PublishedStatus;
    public string exeJs = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        PublishedStatus = Request.QueryString["IsPublished"];
        if (!IsPostBack)
        {
            BindPageData("");
        }
        ClientScript.RegisterStartupScript(typeof(string), "exejs", "<script>" + exeJs + "</script>");
    }

    public void BindPageData(string whereSql)
    {
        if (!string.IsNullOrEmpty(PublishedStatus) && int.Parse(PublishedStatus) == 0)
        {
            whereSql += "  and  IsAvailable=0";
            exeJs = "swithTab('tab2')";
        }
        else
        {
            whereSql += " and IsAvailable=1 ";
            exeJs = "swithTab('tab1')";
        }
        UserModule um = new UserModule();
        DataSet ds=new DataSet();
        ds = um.GetUserListByWhere(whereSql, this.SchoolID);
        Repeater1.DataSource = getPage(ds);
        Repeater1.DataBind();
    }
    public string GetSchNameByID(int schid)
    {
        TeacherModule ta = new TeacherModule();
        string name = ta.GetSchNameByID(schid);
        return name;
    }

    public void SeacheUser_Click(object sender, EventArgs e)
    {
        string where = "";
        if (ddlCheck.SelectedValue == "1")
        {
            where = "  and VU_realname like '%" + TxtCheckName.Text.Trim() + "%'";
        }
        else if (ddlCheck.SelectedValue == "2")
        {
            where = "  and VU_loginname like '%" + TxtCheckName.Text.Trim() + "%'";
        }
        BindPageData(where);
    }
 
    public PagedDataSource getPage(DataSet ds)
    {
        this.pager.RecordCount = ds.Tables[0].Rows.Count;
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.CurrentPageIndex = pager.CurrentPageIndex - 1;
        pds.PageSize = pager.PageSize;
        return pds;
    }
    protected void pager_PageChanged(object sender, EventArgs e)
    {
        SeacheUser_Click(sender, e);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddUser.aspx");
    }
    public void btnUpdate_Click(object sender, CommandEventArgs e)
    {
        Response.Redirect("UpdateUser.aspx?UID=" + e.CommandArgument.ToString());
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
      
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;

            string IsAvailable = drv["IsAvailable"].ToString();
            int schoolid = this.CurrentUser.SchoolID;
            LinkButton btnAvailable = (LinkButton)e.Item.FindControl("btnAvailable");
            btnAvailable.Text = (IsAvailable == "True" ? "禁用" : "启用");
        }

    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {        
        if (e.CommandName.ToLower() == "setavailable")
        {
            string argValue = e.CommandArgument.ToString();
            UserModule um=new UserModule();   
            um.UpdateUserIsAvailable(this.SchoolID, argValue.Split('|')[1] == "False" ? 1 : 0, int.Parse(argValue.Split('|')[0]));

        }
        Response.Redirect("ViewUserInfoList.aspx");

    }
}
